#include <string>
#include <ios>
#include <jni.h>
#include <__locale>
#include <locale>

static JavaVM *g_jvm = nullptr;
static jclass classLocalStoragePlugin = nullptr;


class __EnvGet{
private:
    bool isAttacked = false;
public:
    __EnvGet(JNIEnv **env){
        if(g_jvm){
            int status = g_jvm->GetEnv((void**)env,JNI_VERSION_1_6);
            if(status<0){
                status = g_jvm->AttachCurrentThread(env, nullptr);
                if(status>=0) {
                    isAttacked = true;
                }
            }
        }
    }
    ~__EnvGet(){
        if(g_jvm){
            if(isAttacked)
                g_jvm->DetachCurrentThread();
        }
    }
};


#define DART_API extern "C" __attribute__((visibility("default"))) __attribute__((used))

char * m = "123";


jclass getGlobalClass(JNIEnv* env,const char* name){
    jclass cls = env->FindClass("com/gqshbh/www/plugin/LocalStoragePlugin");
    jclass gcls = (jclass)env->NewGlobalRef(cls);
    env->DeleteLocalRef(cls);
    return gcls;
}

void initClasses(){
    JNIEnv *env = nullptr;
    __EnvGet eg(&env);
    classLocalStoragePlugin = getGlobalClass(env,"com/gqshbh/www/plugin/LocalStoragePlugin");
}

extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved){
    g_jvm = vm;
    initClasses();
    m = "456";
    return JNI_VERSION_1_6;
}

extern "C"
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
{
}


jobject getGlobalContext(JNIEnv *env)
{
    //获取Activity Thread的实例对象
    jclass activityThread = env->FindClass("android/app/ActivityThread");
    jmethodID currentActivityThread = env->GetStaticMethodID(activityThread, "currentActivityThread", "()Landroid/app/ActivityThread;");
    jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread);
    //获取Application，也就是全局的Context
    jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;");
    jobject context = env->CallObjectMethod(at, getApplication);
    return context;
}


DART_API int testAdd(int a,int b){
    return a + b;
}

DART_API const char* getLocalStorage(char* key){
    JNIEnv *env = nullptr;
    __EnvGet eg(&env);
    jobject ctx = getGlobalContext(env);
    jstring ok = env->NewStringUTF(key);
    jmethodID getM =  env->GetStaticMethodID(classLocalStoragePlugin,"get","(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;");
    jobject ov = env->CallStaticObjectMethod(classLocalStoragePlugin,getM,ctx,ok);
    jstring os = (jstring)ov;
    const char*  value = env->GetStringUTFChars(os, JNI_FALSE);
    env->DeleteLocalRef(ok);
    env->DeleteLocalRef(os);
    return value;
}

DART_API void setLocalStorage(char* key,char* value){
    JNIEnv *env = nullptr;
    __EnvGet eg(&env);
    jobject ctx = getGlobalContext(env);
    jstring ok = env->NewStringUTF(key);
    jstring ov = env->NewStringUTF(value);
    //jclass classLocalStoragePlugin = env->FindClass("com/example/gqypf/plugin/LocalStoragePlugin");
    jmethodID setM =  env->GetStaticMethodID(classLocalStoragePlugin,"set","(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V");
    env->CallStaticVoidMethod(classLocalStoragePlugin,setM,ctx,ok,ov);
    env->DeleteLocalRef(ok);
    env->DeleteLocalRef(ov);
}